<?php
	class Phieudatphong_model extends CI_Model 
	{
	    function GetAll() 
		{
        	$post_per_page = 20;

	        $this->db->select('pd.*, MIN(gt.Ngay) AS Ngay');
    	    $this->db->from('PhieuDatPhong pd');
			$this->db->join('GiaThueTheoNgay gt', 'pd.MaPD = gt.PhieuDatPhong');
			$this->db->group_by('gt.PhieuDatPhong');
        	$this->db->limit($post_per_page, $this->uri->segment(3));
	        $this->db->order_by("pd.NgayDatPhong desc, gt.Ngay asc");
    	    $query = $this->db->get();

        	$this->load->library('pagination');
	        $config['base_url'] = base_url() . 'index.php/phieudatphong/admin_index';
    	    $config['total_rows'] = $this->db->get('PhieuDatPhong')->num_rows();
        	$config['per_page'] = $post_per_page;
	        $this->pagination->initialize($config);

    	    if ($query->num_rows() > 0) 
			{
            	foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function GetARecord($mapd) 
		{
        	$query = $this->db->get_where('PhieuDatPhong', array('MaPD' => $mapd));
	        if ($query->num_rows() > 0) 
			{
            	foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}

	    function LayPhieuDat_LoaiPhong_KhachSan($mapd) 
		{
        	$this->db->select('PhieuDatPhong.*, LoaiPhong.Ten AS TenLoaiPhong, KhachSan.MaKS, KhachSan.Ten AS TenKhachSan, KhachSan.VAT, KhachSan.PhiDichVu');
	        $this->db->from('PhieuDatPhong');
    	    $this->db->join('LoaiPhong', 'PhieuDatPhong.LoaiPhong = LoaiPhong.MaLP');
        	$this->db->join('KhachSan', 'LoaiPhong.KhachSan = KhachSan.MaKS');
	        $this->db->where('PhieuDatPhong.MaPD', $mapd);
    	    $query = $this->db->get();
        	if ($query->num_rows() > 0) 
			{
            	foreach ($query->result() as $row) 
				{
                	$data[] = $row;
            	}
	            return $data;
    	    }
        	return NULL;
	    }

    	function Tim($key) 
		{
	        $post_per_page = 20;
    	    $num = 0;
        	if ($this->uri->segment(4))
            	$num = $this->uri->segment(4);

	        $this->db->select('pd.*, MIN(gt.Ngay) AS Ngay');
    	    $this->db->from('PhieuDatPhong pd');
			$this->db->join('GiaThueTheoNgay gt', 'pd.MaPD = gt.PhieuDatPhong');
        	$this->db->where('pd.MaPD', $key);
	        $this->db->or_where('pd.DienThoai', $key);
			$this->db->group_by('gt.PhieuDatPhong');			
    	    $this->db->limit($post_per_page, $num);
        	$this->db->order_by("pd.NgayDatPhong", "desc");
	        $query = $this->db->get();

	        $this->db->select('*');
    	    $this->db->from('PhieuDatPhong');
	        $this->db->where('MaPD', $key);
    	    $this->db->or_where('DienThoai', $key);
        	$query_total = $this->db->get();

	        $this->load->library('pagination');
    	    $config['uri_segment'] = 4;
        	$config['base_url'] = base_url() . 'index.php/phieudatphong/Tim/' . $key;
	        $config['total_rows'] = $query_total->num_rows();
    	    $config['per_page'] = $post_per_page;
        	$this->pagination->initialize($config);

	        if ($query->num_rows() > 0) 
			{
            	foreach ($query->result() as $row) 
				{
                	$data[] = $row;
	            }
    	        return $data;
        	}
	        return NULL;
    	}
		
		function LayMaPD_ajax($key)
		{
        	$this->db->select('MaPD AS Ten');
	        $this->db->from('PhieuDatPhong');
    	    $this->db->like('MaPD', $key);
			$this->db->order_by('MaPD', "asc");
        	$query = $this->db->get();
	        if ($query->num_rows() > 0) 
			{
	            foreach ($query->result() as $row) 
				{
                	$data[] = $row;
            	}
	            return $data;
    	    }
        	return NULL;			
		}
		
		function LaySDT_ajax($key)
		{
        	$this->db->select('DienThoai AS Ten');
	        $this->db->from('PhieuDatPhong');
    	    $this->db->like('DienThoai', $key);
			$this->db->order_by('DienThoai', "asc");
			$this->db->distinct();
        	$query = $this->db->get();
	        if ($query->num_rows() > 0) 
			{
	            foreach ($query->result() as $row) 
				{
                	$data[] = $row;
            	}
	            return $data;
    	    }
        	return NULL;			
		}		
		
    	function LayMaLoaiPhong($mapd) 
		{
        	$this->db->select('LoaiPhong');
	        $this->db->from('PhieuDatPhong');
    	    $this->db->where('MaPD', $mapd);
        	$query = $this->db->get();
	        if ($query->num_rows() > 0) 
			{
	            foreach ($query->result() as $row) 
				{
                	$malp = $row->LoaiPhong;
            	}
	            return $malp;
    	    }
        	return NULL;
	    }

    	function Xoa($mapd, $xoa) 
		{
        	$loaiphong = $this->LayMaLoaiPhong($mapd);

	        $this->load->model('giathuetheongay_model');
    	    $giathuetheongay = $this->giathuetheongay_model->LayGiaThue_CuaMotPhieuDat($mapd);

        	$this->load->model('chitietphieudat_model');
	        $chitietphieudat = $this->chitietphieudat_model->LayChiTiet($mapd);

    	    $this->load->model('phongtrongtheongay_model');
			
			if($giathuetheongay)
			{
	        	foreach ($giathuetheongay as $row1) 
				{
        	    	$magt = $row1->MaGT;
	        	    $ngay = $row1->Ngay;

    	        	foreach ($chitietphieudat as $row2) 
					{
    	            	$mact = $row2->MaCT;
	    	            $loaigiuong = $row2->LoaiGiuong;

    	    	        $sophong = $this->phongtrongtheongay_model->LaySoPhong($ngay, $loaiphong, $loaigiuong);
        	    	    $sophong = $sophong + 1;

            	    	$data = array('SoLuongPhongTrong' => $sophong);
	                	$this->phongtrongtheongay_model->CapNhat($data, $ngay, $loaiphong, $loaigiuong);

	        	        if ($xoa == 1)
    		                $this->chitietphieudat_model->Xoa($mact);
					}
	
    	        	if ($xoa == 1)
        	        	$this->giathuetheongay_model->Xoa($magt);
				}
	        }

    	    if ($xoa == 1)
			{
				return $this->db->delete('PhieuDatPhong', array('MaPD' => $mapd));
			}
	        return 1;
    	}

	    function LayEmail($mapd) 
		{
        	$phieudatphong = $this->GetARecord($mapd);
	        $email = NULL;
    	    if ($phieudatphong) 
			{
	            foreach ($phieudatphong as $row)
    	            $email = $row->Email;
        	    return $email;
	        }
    	    return $email;
	    }

	    function CapNhat($data, $mapd) 
		{
	        $this->db->update('PhieuDatPhong', $data, array('MaPD' => $mapd));
    	    return $this->db->affected_rows();
	    }

    	function CapNhatTinhTrang($state, $mapd) 
		{
        	if ($state == -1) 
			{
            	$xoa = 0;
	            $this->Xoa($mapd, $xoa);
    	    }
        	$this->db->update('PhieuDatPhong', array('TinhTrangThanhToan' => $state), array('MaPD' => $mapd));
	        return $this->db->affected_rows();
    	}

	    function XoaCacPhieuChuaThanhToan() 
		{
	        $strSQL = "SELECT MaPD FROM PhieuDatPhong WHERE TinhTrangThanhToan = 0 AND TIMESTAMPDIFF(HOUR, NgayDatPhong, NOW()) > 24";
    	    $query = $this->db->query($strSQL);

           	$result = 1;
        	if ($query->num_rows() > 0) 
			{
	            foreach ($query->result() as $row) 
				{
	                $mapd = $row->MaPD;

    	            $xoa = 1;
        	        $result = $this->Xoa($mapd, $xoa);

            	    if (!$result)
                	    return $result;
	            }
    	        return $result;
        	}
			else
				return $result;
	    }

	    function add_records_phieudatphong($data) 
		{
        	$this->db->insert('PhieuDatPhong', $data);
	        return $this->db->insert_id();
    	}
	}
?>